'use strict';
const fs = require("fs"), xml = require("xml-js"), images = require("images"), sharp = require('sharp'), bpath = "D:/github/sgame/tool/plist/";
p2m(["fish_move1.plist", "fish_move2.plist", "fish_dead.plist", "bullet_guns_coins.plist"]);

/** cocos plist 动画转 拆分成小图 */
function p2m(farr){
    for(let f of farr){
        fs.readFile(bpath + f, (err,bu)=>{
            if(err){
                console.log(err);
                return;
            }
            let jn = xml.xml2js(bu, {compact: true});
            console.log(jn);
            let imgObj = sharp(bpath + jn.plist.dict.dict[1].string[0]._text);
            console.log("basefile");
            for(let i=0;i<jn.plist.dict.dict[0].key.length;i++){
                let pos = jn.plist.dict.dict[0].dict[i].string[0]._text;
                pos = pos.substr(2, pos.length-4).replace("},{",",").split(",");
                console.log(jn.plist.dict.dict[0].key[i]._text, pos);

                try{
                    if("true" in jn.plist.dict.dict[0].dict[i]){
                        imgObj.clone().extract({ left: pos[0]|0, top: pos[1]|0, width: pos[3]|0, height: pos[2]|0 }).rotate(-90).toFile(bpath + "m/"+ jn.plist.dict.dict[0].key[i]._text, (err, info)=>{
                            if(err) console.log("error", err);
                        });
                    }else{
                        imgObj.clone().extract({ left: pos[0]|0, top: pos[1]|0, width: pos[2]|0, height: pos[3]|0 }).toFile(bpath + "m/"+ jn.plist.dict.dict[0].key[i]._text);
                    }
                }catch(e){
                    console.error("save", pos[0], pos[1], pos[2], pos[3], e);
                }
            }
        });
    }
}

/** cocos plist 动画转 egret json文件 */
function p2e(farr){
    for(let f of farr){
        fs.readFile(bpath + f, (err,bu)=>{
            if(err){
                console.log(err);
                return;
            }
            let jn = xml.xml2js(bu, {compact: true});
            console.log(jn);
            let ejn = {mc:{
                            run:{frameRate:24,labels:[],frames:[]}
                        },
                        file:jn.plist.dict.dict[1].string[0]._text,
                        res:{}};
            let fn = "", frame = 0;
            console.log("basefile");
            for(let i=0;i<jn.plist.dict.dict[0].key.length;i++){
                let pos = jn.plist.dict.dict[0].dict[i].string[0]._text;
                pos = pos.substr(2, pos.length-4).replace("},{",",").split(",");
                console.log(jn.plist.dict.dict[0].key[i]._text, pos);

                ejn.res[jn.plist.dict.dict[0].key[i]._text] = {x:pos[0],y:pos[1],w:pos[2],h:pos[3],r:("true" in jn.plist.dict.dict[0].dict[i])};

                pos = jn.plist.dict.dict[0].dict[i].string[2]._text;
                pos = pos.substr(2, pos.length-4).replace("},{",",").split(",");
                ejn.mc.run.frames.push({res:jn.plist.dict.dict[0].key[i]._text,x:pos[0],y:pos[1]});

                if(fn && jn.plist.dict.dict[0].key[i]._text.indexOf(fn) == 0){
                    continue;
                }else{
                    fn = jn.plist.dict.dict[0].key[i]._text;
                    if(ejn.mc.run.labels.length > 0){
                        ejn.mc.run.labels[ejn.mc.run.labels.length-1].end = ejn.mc.run.frames.length-2;
                    }
                }
                let pl = fn.lastIndexOf("_");
                if(pl != -1){
                    fn = fn.substr(0, pl);
                }
                ejn.mc.run.labels.push({name:fn,frame:frame,end:ejn.mc.run.frames.length-1});
                frame = ejn.mc.run.frames.length;
                if(frame > 20) break;
            }
            console.log(JSON.stringify(ejn));
        });
    }
}